package score;

import beans.SessionManagedBean;
import java.util.AbstractMap.SimpleEntry;
import java.util.ArrayList;
import java.util.List;
import java.util.Map.Entry;
import javax.faces.bean.CustomScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import lecture.Lecture;
import task.ExerciseSheet;
import task.ExerciseSheetManagedBean;
import task.Task;
import user.User;

/**
 *
 * @author Thorsten Ludwig
 */

@ManagedBean(name="viewScoreBean")
@CustomScoped(value="#{window}")
public class ViewScoreManagedBean {
    
    List<ExerciseSheetManagedBean> beans;
    
    public Entry<Double,Double> sumScore(ExerciseSheet es, String username) {
        if (username.equals("")) {
            FacesContext c = FacesContext.getCurrentInstance();
            SessionManagedBean sb = c.getApplication().evaluateExpressionGet(c, "#{sessionBean}", SessionManagedBean.class);
            username = sb.getUser().getUsername();
        }
        List<Score> score = this.getScore(es, username);       
        double sum=0;
        double max = 0;
        for (Score s: score) {
            sum = sum + s.getScore();
        }
        
        for (Task t: es.getExercises()) {
            max = + t.getMaxScore();
        }
        
        return new SimpleEntry<Double,Double>(sum,max);
    }
    
    public List<Score> getScore(ExerciseSheet es) {
        FacesContext context = FacesContext.getCurrentInstance();
        SessionManagedBean sb = context.getApplication().evaluateExpressionGet(context, "#{sessionBean}", SessionManagedBean.class);
        User user = sb.getUser();
        return this.getScore(es, user.getUsername());
    }
    
    public List<Score> getScore(ExerciseSheet es, String username) {
        FacesContext context = FacesContext.getCurrentInstance();
        SessionManagedBean sb = context.getApplication().evaluateExpressionGet(context, "#{sessionBean}", SessionManagedBean.class);
        Lecture lecture = sb.getLectureBean().getLecture();
        List<Entry<String, List<Score>>> scoreBySheet = lecture.getScoreBySheet(es);
        List<Score> score = null;
        for (Entry<String, List<Score>> e: scoreBySheet) {
            if (e.getKey().equals(username))
                score = e.getValue();
        }
        if (score == null) 
            System.out.println("score ist null :-(");
        return score;
    }
    
    public List<ExerciseSheetManagedBean> getSheetBeans() {
        if (this.beans == null) {
            List<ExerciseSheetManagedBean> list = new ArrayList<ExerciseSheetManagedBean>();
            FacesContext context = FacesContext.getCurrentInstance();
            SessionManagedBean sb = (SessionManagedBean) context.getApplication().evaluateExpressionGet(context, "#{sessionBean}", SessionManagedBean.class);
            Lecture lecture = sb.getLectureBean().getLecture();

            for (ExerciseSheet es: lecture.getExerciseSheets()) {
                list.add(new ExerciseSheetManagedBean(es));
            }
            this.beans = list;
        }   
        return this.beans;
    }
    
}
